﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using System;
using DG.Tweening;

public class TurnTable : MonoBehaviour
{
    #region
    //public float endAngle = 100;//旋转停止位置，相对y坐标方向的角度
    //public Vector3 targetDir;//目标点的方向向量
    //bool isMoving = false;//是否在旋转
    //public float speed = 0;//当前的旋转速度
    //public float maxSpeed = 10;//最大旋转速度
    //public float minSpeed = 0.8f;//最小旋转速度
    //float rotateTimer = 2;//旋转计时器
    //public int moveState = 0;//旋转状态，旋转，减速
    //public int keepTime = 3;//旋转减速前消耗的时间
    ////按照顺时针方向递增
    //string[] rewards = { "8等奖", "7等奖", "6等奖", "5等奖", "4等奖", "3等奖", "2等奖", "1等奖", "10等奖", "9等奖" };
    
    // Use this for initialization
    public Button btn1;
    public Button btn2;
    new Animation animation;
    public Animator anit;
    public bool abc;
    AnimationEvent ae;
    AnimationClip ac;
    List<AnimationEvent> aws = new List<AnimationEvent>();
    #endregion
    bool isRotate=true;
    float rotateTimer = 0;
    float time = 4;
    void Start()
    {
        #region
        animation = transform.GetComponent<Animation>();
        ac = animation.GetClip("TurnNormal");
        btn1.onClick.AddListener(OnClick1);
        btn2.onClick.AddListener(OnClick3);
        ae = new AnimationEvent();
        #endregion

    }
    #region
    private void OnClick3()
    {
        #region
        ae.functionName = "OnClick2";
        ae.time = 0.5f;
        if (ae != null)
            ac.AddEvent(ae);
        #endregion
    }

    private void OnClick2()
    {
        #region
        animation.Stop("TurnNormal");
        animation.Play("1");
        #endregion
    }

    private void OnClick1()
    {
       // animation.Play("TurnNormal");

    }
    #endregion
    // Update is called once per frame
    void Update()
    {
        if (!isRotate) return;

        rotateTimer += Time.deltaTime;
        if (rotateTimer < time)
        {
            transform.Rotate(Vector3.back * 300 * Time.deltaTime);
        }
        else
        {
            //结束，使用DoTween旋转到结束角度，耗时1秒
            //这里有个360，使用来防止指针回转的，如果不加这个360，你会看到指针倒退
            transform.DORotate(new Vector3(0, 0, -(360 + 40)), 4f, RotateMode.FastBeyond360);
            rotateTimer = 0;
            isRotate = false;
        
        }
    }
    #region
    //float GetAngle()
    //{
    //    return CalAngle(targetDir, transform.up);//计算y轴方向的旋转角度
    //}
    ////计算从dir1旋转到dir2的角度

    //float CalAngle(Vector3 dir1, Vector3 dir2)
    //{
    //    float angle = Vector3.Angle(dir1, dir2);
    //    Vector3 normal = Vector3.Cross(dir1, dir2);
    //    //		Debug.Log ("normal="+normal);
    //    //		angle = normal.z > 0 ? angle : (180+(180-angle));
    //    angle = normal.z > 0 ? angle : (360 - angle);
    //    return angle;
    //}

    ///// <summary>
    ///// 计算目标位置的向量
    ///// Calculates the dir.
    ///// </summary>
    ///// <param name="endAngle">End angle.</param>
    //Vector3 CalculateDir(float endAngle)
    //{
    //    float radiansX = Mathf.Cos(Mathf.PI * (endAngle + 90) / 180);
    //    float radiansY = Mathf.Sin(Mathf.PI * (endAngle + 90) / 180);
    //    return new Vector3(radiansX, radiansY, 0);

    //}
    //void EndMove()
    //{

    //    speed = 0;
    //    isMoving = false;
    //    moveState = 0;
    //}

    //void StartMove()
    //{
    //    if (isMoving)
    //        return;
    //    int index = Random.Range(0, 10) - 1;
    //    Debug.Log("恭喜你获得" + rewards[index]);
    //    endAngle = index * 360 / 10;//获得目标位置相对y坐标方向的角度
    //    targetDir = CalculateDir(endAngle);//获得目标位置方向向量
    //    rotateTimer = keepTime;
    //    isMoving = true;
    //    moveState = 1;
    //}
    #endregion
}
